/*
 * Copyright 2013 Joan Zapata
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ksy.common.utils;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class BaseAdapter<T, K extends BaseHolder> extends RecyclerView.Adapter<K> {

    private int totalSize = -1;

    @Override
    public K onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate;
        if (itemLayoutId > 0) {
            inflate = LayoutInflater.from(parent.getContext()).inflate(itemLayoutId, parent, false);
        } else {
            inflate = itemLayoutView;
        }
        BaseHolder k = new BaseHolder(inflate);
        return (K) k;
    }

    @Override
    public void onBindViewHolder(K holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemLongClickListener != null) {
                    mOnItemLongClickListener.onItemClick(v, position);
                }
                return false;
            }
        });
        convert(holder, position);
    }

    @Override
    public int getItemCount() {
        if (totalSize <= 0) {
            return mList.size();
        }
        return totalSize;
    }

    protected OnItemClickListener mOnItemClickListener;
    protected OnItemClickListener mOnItemLongClickListener;
    protected int itemLayoutId;
    protected List<T> mList;
    private View itemLayoutView;

    public BaseAdapter(@LayoutRes int layoutResId, @Nullable List<T> data) {
        this.itemLayoutId = layoutResId;
        this.mList = data;
    }

    public BaseAdapter(@LayoutRes int layoutResId, @Nullable List<T> data, int totalSize) {
        this.totalSize = totalSize;
        this.itemLayoutId = layoutResId;
        this.mList = data;
    }

    public BaseAdapter(View layout, @Nullable List<T> data) {
        this.itemLayoutView = layout;
        this.mList = data;
    }

    public BaseAdapter(View layout, @Nullable List<T> data, int totalSize) {
        this.totalSize = totalSize;
        this.itemLayoutView = layout;
        this.mList = data;
    }

    public void notifyDataListChanged(@Nullable List<T> data) {
        this.mList = data;
        notifyDataSetChanged();
    }

    protected abstract void convert(K helper, int position);

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(@Nullable OnItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public OnItemClickListener getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public OnItemClickListener getOnItemLongClickListener() {
        return mOnItemLongClickListener;
    }

    public void setOnItemLongClickListener(OnItemClickListener mOnItemLongClickListener) {
        this.mOnItemLongClickListener = mOnItemLongClickListener;
    }

}
